/**
 * @param {number[][]} rooms
 * @return {boolean}
 */
var canVisitAllRooms = function(rooms) {
  let visited = new Array(rooms.length).fill(false), keyArray = [...rooms[0]];
  visited[0] = true;
  while(keyArray.length) {
    const key = keyArray.shift();
    for(let i = 0; i < rooms[key].length; i++) {
      if(visited[rooms[key][i]]) continue;
      keyArray.push(rooms[key][i]);
      visited[rooms[key][i]] = true;
    }
    visited[key] = true;
  }
  if(visited.includes(false)) return false;
  return true;
};
let rooms = [[1],[2],[3],[]];
console.log(canVisitAllRooms(rooms));
rooms = [[1,3],[3,0,1],[2],[0]];
console.log(canVisitAllRooms(rooms));
